var wxpay = require('../../utils/pay.js')
var app = getApp()
var formatTime = require('../../utils/util.js')
Page({
    data:{
      orderId:0,
      order: {},
      imgUrl: app.globalData.imgUrl,
      goodsList:[],
      yunPrice:"0.00",
      hasLogistics: true,
      statusTips: "",
      interval: '',
      restTime: ''
    },
    onLoad:function(e){
      let order = JSON.parse(e.order)
      console.log(order)
      order.orderinfo.contact.address = order.orderinfo.contact.province + order.orderinfo.contact.city + order.orderinfo.contact.district + order.orderinfo.contact.address
      order.orderinfo.create_time = formatTime.formatTime(new Date(order.orderinfo.created_at))
      this.setData({
        order: order
      });
      this.showStatus(order.orderinfo.pay_status, order.orderinfo.status)
    },
    onShow : function () {
      var that = this;
  
      var yunPrice = parseFloat(this.data.yunPrice);
      var allprice = 0;
      var goodsList = this.data.goodsList;
      for (var i = 0; i < goodsList.length; i++) {
        allprice += parseFloat(goodsList[0].price) * goodsList[0].number;
      }
      this.setData({
        allGoodsPrice: allprice,
        yunPrice: yunPrice
      });
    },

  toLogicticsDetails: function () {
    let that = this
    wx.navigateTo({
      url: "/pages/logictics-details/index?id=" + that.data.order.orderinfo.id
    })
  },

  toPay: function () {
    wxpay.wxpay(app, this.data.order.orderinfo.id, "/pages/order-list/index");
  },

  /**
   * 取消订单
   */
  cancel: function (e) {
    let id = e.currentTarget.dataset.id
    let token = wx.getStorageSync('token')
    let that = this
    wx.request({
      url: app.apiUrl('/order/cancel'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        id: id
      },
      success: function (res) {
        console.log(res.data.data)
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 1)
        } else {
          wx.navigateBack({
            delta: 1
          })
        }

      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  },

  /**
   * 显示订单状态
   */
  showStatus: function (payStatus, status) {
    let showTips = ""
    let that = this
    if (payStatus == 0 && status == 0) {//未支付，待发货
      this.data.interval = setInterval(function () {
        let date = new Date(that.data.order.orderinfo.create_time)
        date.setTime(date.getTime() + 24 * 60 * 60 * 1000)
        let restTime = formatTime.restTime(new Date(), date)

        that.setData({
          statusTips: "订单剩余支付时间:",
          restTime: restTime.hour + "小时" + restTime.minute + "分" + restTime.second + "秒"
        })

      }, 1000)

    }
    else if (payStatus == 0 && status == 3) {//未支付，已关闭
      that.setData({
        statusTips: "订单状态：交易已关闭"
      })  
    }
    else if (payStatus == 1 && status == 0) {//已支付，待发货
      that.setData({
        statusTips: "订单状态：等待发货"
      })  
    }
    else if (payStatus == 1 && status == 1) {//已支付，已发货
      that.setData({
        statusTips: "订单状态：等待收货"
      })   
    }
    else if (payStatus == 1 && status == 2) {//已支付，已完成
      that.setData({
        statusTips: "订单状态：已完成"
      })   
    }
    else if (payStatus == 1 && status == 4) {//已支付，待退款
      that.setData({
        statusTips: "等待退款"
      })  
    }
    else if (payStatus == 1 && status == 5) {//已支付，已退款
      that.setData({
        statusTips: "订单已经退款"
      })   
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this
    clearInterval(that.data.interval)
  },

  /**
   * 取消订单
   */
  cancel: function (e) {
    let id = e.currentTarget.dataset.id
    let token = wx.getStorageSync('token')
    let that = this
    wx.request({
      url: app.apiUrl('/order/cancel'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        id: id
      },
      success: function (res) {
        console.log(res.data.data)
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 1)
        } else {
          that.onShow()
        }

      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  },
  /**
   * 未支付订单付款 
   */
  pay: function (e) {
    let index = e.currentTarget.dataset.index
    let order = this.data.orderList[index]
    wx.navigateTo({
      url: "/pages/pay-select/index?orderid=" + order.orderinfo.id + "&money=" + order.orderinfo.total_price_pay + "&createtime=" + order.orderinfo.created_at + "&fromID=1"
    })
  },

  toDetailsTap: function (e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id + "&type=0"
    })
  }


})